﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zzva.Common; 

namespace Zzva.Report
{
    //описвает поле сущность
    public class TReportFieldEntity:TReportFieldBase 
    {


        private TReportListField mvarReportListField;


        public override TReportFieldBase GetField(string pOboznFull)
        {
            try
            {

                int lPozFirstPoint;
                TReportFieldBase lReportFieldBase = null;
                int lPozSecondPoint;
                string lChildObozn;
                string lChildOboznFull;

                ////string lObozn;



                if (pOboznFull == "") { throw (new CommonException("Не задано обозначение поля")); }


                //ищем точку
                lPozFirstPoint = pOboznFull.IndexOf('.');

                if (lPozFirstPoint == -1)//это узел
                {
                    if (pOboznFull == Obozn) { lReportFieldBase = this; }
                    else { throw (new CommonException("Поле не найдено")); }

                }
                else // ищем нижележащий узел
                {
                    //отделяем оставшуюся строку и ищем в ней точку
                    lPozSecondPoint = pOboznFull.IndexOf('.', lPozFirstPoint + 1);


                    if (lPozSecondPoint == -1)
                    {
                        lChildObozn = pOboznFull.Substring(lPozFirstPoint + 1);
                        lChildOboznFull = lChildObozn;

////                        lReportFieldBase = mvarReportListField.FirstOrDefault(h => h.Obozn == lChildObozn);
////
////                        if (lReportFieldBase != null){lReportFieldBase = lReportFieldBase.GetField(lChildOboznFull);  }
////                        else{throw (new CommonException("Поле не найдено"));}
                    }
                    else
                    {
                        lChildObozn = pOboznFull.Substring(lPozFirstPoint + 1, lPozSecondPoint - lPozFirstPoint - 1);
                        lChildOboznFull = pOboznFull.Substring(lPozFirstPoint + 1);

////                        lReportFieldBase = mvarReportListField.FirstOrDefault(h => h.Obozn == lChildObozn);
////
////                        if (lReportFieldBase != null) { lReportFieldBase = lReportFieldBase.GetField(lChildOboznFull); }
////                        else { throw (new CommonException("Поле не найдено")); }
                    }


                    lReportFieldBase = mvarReportListField.FirstOrDefault(h => h.Obozn == lChildObozn);

                    if (lReportFieldBase != null) { lReportFieldBase = lReportFieldBase.GetField(lChildOboznFull); }
                    else { throw (new CommonException("Поле не найдено")); }



                }






                return lReportFieldBase;

            }




            catch (Exception e1) { throw e1; }
            finally { }
        }




        public override int GetColFieldValue()
        {
            try
            {

                int lColFieldValue = 0;
                int lColFieldValueCur;

                foreach (TReportFieldBase lReportField in mvarReportListField)
                {
                    lColFieldValueCur = lReportField.GetColFieldValue();
                    lColFieldValue = lColFieldValue + lColFieldValueCur;
                }

                return lColFieldValue;


            }

            catch (Exception e1) { throw e1; }
            finally { }
        }




        public override int GetColLevel()
        {
            try
            {
                int lColLevel = 0;
                int lColLevelCur;

                foreach (TReportFieldBase lReportField in mvarReportListField)
                {
                    lColLevelCur = lReportField.GetColLevel();
                    if (lColLevelCur > lColLevel) { lColLevel = lColLevelCur; }
                }

                lColLevel = lColLevel + 1;//добавляем свой уровень

                return lColLevel;

            }
            catch (Exception e1) { throw e1; }
            finally { }
        }












        public TReportFieldEntity(string pObozn, string pOboznFull, string pNaim, TReportListField pReportListField)
            : base(pObozn,pOboznFull,pNaim)
        {

            mvarReportListField = pReportListField;

        }


        public TReportListField ReportListField
        {
            get{return mvarReportListField;}
            set{mvarReportListField = value;}
        }






    }
}
